﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.Data;
using BEGDC.Utility.NHibernate;
using Castle.Services.Transaction;
using System.Collections;
using System.Xml;
using Castle.Facilities.NHibernateIntegration;

namespace BEGDC.PreContract.BLL
{
	[Transactional]
	public partial class DicItem: IDicItem
	{
		private  ISessionFactory  factory;

        /// <summary>
        /// Initiate session factory.
        /// </summary>
        /// <param name="sessionFactory"></param>
        public DicItem(ISessionFactory sessionFactory)
        {
            this.factory = sessionFactory;
        }
		
		//Get by id
		[Transaction(TransactionMode.Requires)]
		public BEGDC.PreContract.DAL.DicItem GetDicItem(System.Int32 dicItemID)
		{
			BEGDC.PreContract.DAL.DicItem dicItem = BEGDC.PreContract.DAL.DicItem.TryFind(dicItemID);

            return dicItem;
		}
		
		//Get by filter
		[Transaction(TransactionMode.Requires)]
		public DataTable GetDicItem_DataTable(string filter)
		{
			using (ISession session = factory.OpenSession())
            {
				string strSql = "SELECT * FROM DicItem";
				if (filter.Trim() != string.Empty)
					strSql += " WHERE " + filter;

                DataTable dtSearch = DAO.ExecuteDataTable(session, strSql);

                return dtSearch;
            }
		}
		
		//Delete
		[Transaction(TransactionMode.Requires)]
		public void DeleteDicItem(System.Int32 dicItemID)
		{
			try
            {
				BEGDC.PreContract.DAL.DicItem dicItem = BEGDC.PreContract.DAL.DicItem.TryFind(dicItemID);
                dicItem.Delete();
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("DicItem", string.Format("Error occured when deleting the DicItem, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Add
		[Transaction(TransactionMode.Requires)]
		public int AddDicItem(BEGDC.PreContract.DAL.DicType dicTypeID, System.String dicItemValue, System.String dicItemDesc, System.Boolean isEnabled)	
		{
			try
            {
				BEGDC.PreContract.DAL.DicItem dicItem = new BEGDC.PreContract.DAL.DicItem();
				
				dicItem.DicTypeID = dicTypeID;
				dicItem.DicItemValue = dicItemValue;
				dicItem.DicItemDesc = dicItemDesc;
				dicItem.IsEnabled = isEnabled;
				dicItem.CreateAndFlush();
				
				return dicItem.DicItemID;
			}
			catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("DicItem", string.Format("Error occured when adding a new DicItem, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Update
		[Transaction(TransactionMode.Requires)]
		public bool UpdateDicItem(System.Int32 dicItemID, BEGDC.PreContract.DAL.DicType dicTypeID, System.String dicItemValue, System.String dicItemDesc, System.Boolean isEnabled)
		{
			try
            {
                BEGDC.PreContract.DAL.DicItem dicItem = GetDicItem(dicItemID);

				dicItem.DicTypeID = dicTypeID;
				dicItem.DicItemValue = dicItemValue;
				dicItem.DicItemDesc = dicItemDesc;
				dicItem.IsEnabled = isEnabled;
				dicItem.Save();
				
				return true;
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("DicItem", string.Format("Error occured when updating the DicItem, error information: {0}.", ex.Message), ex);
            }
		}
	}
}



